package fragment.tabhostFragment;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.TextView;

import com.baoyz.swipemenulistview.SwipeMenu;
import com.baoyz.swipemenulistview.SwipeMenuCreator;
import com.baoyz.swipemenulistview.SwipeMenuItem;
import com.baoyz.swipemenulistview.SwipeMenuListView;
import com.lanou.horo.myapplication.R;

import dbdao.ColletionDBHelper;
import fragment.AbsBaseFragment;
import utils.adapter.HistoryDbAdpter;
import utils.adapter.MyColltionAdpter;

/**
 * Created by dllo on 15/12/1.
 */
public class MyCollectionFragment extends AbsBaseFragment{
    private SwipeMenuListView listView;
    private MyColltionAdpter adpter;
    private ColletionDBHelper helper;
    private ImageView titleForAllBack;
    private TextView titleForAllTv;
    private LayoutInflater inflater;

    @Override
    protected int getLayoutView() {
        return R.layout.fragment_me_colltion;
    }

    @Override
    protected void initView() {
        inflater = getActivity().getLayoutInflater();
        helper = ColletionDBHelper.getInstance();
        listView = findView(R.id.collectionlistview);
        titleForAllBack = findView(R.id.titleForAllBack);
        titleForAllBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                goBack();
            }
        });
        titleForAllTv = findView(R.id.titleForAllTv);
        titleForAllTv.setText("收藏");
    }

    @Override
    protected void initData() {
        adpter = new MyColltionAdpter();
        listView.setAdapter(adpter);
        adpter.addData(inflater, helper.queryAll());

        //滑动删除
        SwipeMenuCreator creator = new SwipeMenuCreator() {
            @Override
            public void create(SwipeMenu menu) {
                // create "open" item
                SwipeMenuItem openItem = new SwipeMenuItem(
                        getContext());
                // set item background
                openItem.setBackground(new ColorDrawable(Color.rgb(0xC9, 0xC9,
                        0xCE)));
                // set item width
                openItem.setWidth(90);
                // set item title
                openItem.setTitle("查看");
                // set item title fontsize
                openItem.setTitleSize(18);
                // set item title font color
                openItem.setTitleColor(Color.WHITE);
                // add to menu
                menu.addMenuItem(openItem);

                // create "delete" item
                SwipeMenuItem deleteItem = new SwipeMenuItem(
                        getContext());
                // set item background
                deleteItem.setBackground(new ColorDrawable(Color.rgb(0xF9,
                        0x3F, 0x25)));
                // set item width
                deleteItem.setWidth((90));
                // set a icon
                deleteItem.setIcon(R.mipmap.ic_delete);
                // add to menu
                menu.addMenuItem(deleteItem);
            }
        };
        // set creator
        listView.setMenuCreator(creator);
        listView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
            @Override
            public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
                switch (index) {
                    case 0:
                        // open
                        break;
                    case 1:
                        helper.deleById(helper.queryAll().get(position).getId());
                        adpter.addData(inflater, helper.queryAll());
                        listView.setAdapter(adpter);
                        break;
                }
                // false : close the menu; true : not close the menu
                return false;
            }
        });
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                goToPlay(helper.queryAll().get(position).getTrackId());
            }
        });
    }


}
